package com.cms.work.controller.sys;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.cms.work.core.SimpleResponse;
import com.cms.work.core.controller.BaseController;
import com.cms.work.entity.enums.CommonEnum.RoleStatusEnum;
import com.cms.work.entity.enums.CommonEnum.UserStatusEnum;
import com.cms.work.entity.sys.SysRole;
import com.cms.work.entity.sys.vo.SysRoleResourceVo;
import com.cms.work.service.sys.ISysResourceService;
import com.cms.work.service.sys.ISysRoleService;
import com.github.pagehelper.PageInfo;

/**
 * <p>Title: SysRoleController</p>
 * <p>Description: 角色控制层</p>
 * <p>Email: yuyue_qty@163.com</p>
 * @author 晴天雨
 * @date 2016年11月23日下午11:00:13
 * @version 1.0
 */
@Controller
@RequestMapping("/sys")
public class SysRoleController extends BaseController<SysRole> {

	private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);
	
	@Autowired
	private ISysRoleService sysRoleService;
	@Autowired
	private ISysResourceService sysResourceService;
	
	@RequestMapping(value="/queryRole", method={RequestMethod.POST, RequestMethod.GET})
	public ModelAndView query(
			@RequestParam(value = "pageNum", defaultValue = PAGE_NUM) int pageNum,
			@RequestParam(value = "pageSize", defaultValue = PAGE_SIZE) int pageSize,
			SysRole sysRole){
//		sysRole.setStatus(RoleStatusEnum.enabled.getStatus());
		if(StringUtils.isBlank(sysRole.getName())) sysRole.setName(null);
		PageInfo<SysRole> pageInfo = sysRoleService.selectPage(pageNum, pageSize, sysRole);
		return new ModelAndView("/sys/role-list").addObject("pageInfo", pageInfo);
	}
	
	@RequestMapping(value="/loadRole", method={RequestMethod.GET})
	public ModelAndView load(@RequestParam(value="roleId", defaultValue="0")int id){
		SysRole role = null;
		if(id == 0){
			role = new SysRole();
		}else{
			role = sysRoleService.selectByPrimaryKey(id);
		}
		List<SysRoleResourceVo> resourceList = sysResourceService.selectRoleResourceByRoleId(id);
		return new ModelAndView("/sys/role-add").addObject("role", role).addObject("resourceList", JSON.toJSONString(resourceList));
	}
	
	@RequestMapping(value="/addRole", method={RequestMethod.POST})
	public String add(SysRole sysRole, Integer[] resources){
		if(resources == null){
			resources = new Integer[0];
		}
		sysRole.setCreateDate(new Date());
		sysRoleService.addRole(sysRole, Arrays.asList(resources));
		return "redirect:/sys/queryRole?id="+sysRole.getId();
	}
	
	@RequestMapping(value="/changeRoleStatus", method={RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public SimpleResponse changeStatus(@RequestParam(value="roleId")int id, int status){
		SimpleResponse result = new SimpleResponse();
		try {
			if(status == UserStatusEnum.disable.getStatus()){
				sysRoleService.updateRoleStatus(id, RoleStatusEnum.disable);
			}else if(status == UserStatusEnum.enabled.getStatus()){
				sysRoleService.updateRoleStatus(id, RoleStatusEnum.enabled);
			}else{
				result.setMsg("角色状态未定义");
				result.setState(SimpleResponse.StateEnum.UNSIGN.getValue());
				return result;
			}
			result.putData("role", sysRoleService.selectByPrimaryKey(id));
			result.setState(SimpleResponse.StateEnum.SUCCESS.getValue());
			result.setMsg("success");
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			result.setState(SimpleResponse.StateEnum.ERROR.getValue());
			logger.error("角色状态修改失败", e);
		}
		return result;
	}
}




